home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir42 / gnudbm14.zip / TESTDBM.C < prev    next >
C/C++ Source or Header  |  1990-08-24  |  6KB  |  235 lines

  1. /* testdbm.c - Driver program to test the dbm interface routines. */
  2.  
  3. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  4.     Copyright (C) 1990  Free Software Foundation, Inc.
  5.  
  6.     GDBM is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 1, or (at your option)
  9.     any later version.
  10.  
  11.     GDBM is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with GDBM; see the file COPYING.  If not, write to
  18.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     You may contact the author by:
  21.        e-mail:  phil@wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.         phone:  (206) 676-3035
  27.        
  28. *************************************************************************/
  29.  
  30. /*
  31.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@@ddagsi3.bitnet
  32.  *
  33.  * To this port, the same copying conditions apply as to the
  34.  * original release.
  35.  *
  36.  * IMPORTANT:
  37.  * This file is not identical to the original GNU release!
  38.  * You should have received this code as patch to the official
  39.  * GNU release.
  40.  *
  41.  * MORE IMPORTANT:
  42.  * This port comes with ABSOLUTELY NO WARRANTY.
  43.  *
  44.  * $Header: e:/gnu/gdbm/RCS/testdbm.c'v 1.4.0.1 90/08/16 09:22:42 tho Exp $
  45.  */
  46.  
  47. #include <stdio.h>
  48. #include <sys/types.h>
  49. #ifndef MSDOS
  50. #include <sys/file.h>
  51. #endif /* not MSDOS */
  52. #include <sys/stat.h>
  53.  
  54. #define TRUE  1
  55. #define FALSE 0
  56.  
  57. #ifdef __STDC__
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include "dbm.h"
  61. #else /* not __STDC__ */
  62. typedef struct {
  63.   char *dptr;
  64.   int   dsize;
  65. } datum;
  66.  
  67. extern datum fetch ();
  68. extern datum firstkey ();
  69. extern datum nextkey ();
  70. #endif /* not __STDC__ */
  71.  
  72. /* The test program allows one to call all the routines plus the hash function.
  73.    The commands are single letter commands.  The user is prompted for all other
  74.    information.  The commands are q (quit), f (fetch), s (store), d (delete),
  75.    1 (firstkey), n (nextkey) and h (hash function). */
  76.  
  77. main (argc, argv)
  78.      int argc;
  79.      char *argv[];
  80. {
  81.  
  82.   char  cmd_ch;
  83.  
  84.   datum key_data;
  85.   datum data_data;
  86.   datum return_data;
  87.  
  88.   char key_line[500];
  89.   char data_line[1000];
  90.  
  91.   char done = FALSE;
  92.   char sys[255];
  93.  
  94.   char *file_name;
  95.  
  96.   /* Argument checking. */
  97.   if (argc > 2)
  98.     {
  99.       printf ("Usage: %s [dbm-file] \n",argv[0]);
  100.       exit (2);
  101.     }
  102.  
  103.   if (argc > 1)
  104.     {
  105.       file_name = argv[1];
  106.     }
  107.   else
  108.     {
  109.       file_name = "junkdbm";
  110.     }
  111.  
  112.   /* Initialize */
  113.   data_data.dptr = data_line;
  114.  
  115.   if (dbminit (file_name) != 0)
  116.     {
  117.       sprintf (sys,"touch %s.pag %s.dir", file_name, file_name);
  118.       system (sys);
  119.       if (dbminit (file_name) != 0)
  120.     {
  121.       printf ("dbminit failed.\n");
  122.       exit (2);
  123.     }
  124.     }
  125.  
  126.   /* Welcome message. */
  127.   printf ("\nWelcome to the dbm test program.  Type ? for help.\n\n");
  128.   
  129.   while (!done)
  130.     {
  131.       printf ("com -> ");
  132.       cmd_ch = getchar ();
  133.       while (getchar () != '\n') /* Do nothing. */;
  134.       switch (cmd_ch)
  135.     {
  136.     case 'q':
  137.       done = TRUE;
  138.       break;
  139.  
  140.     case 'f':
  141.       printf ("key -> ");
  142.       gets (key_line);
  143.       key_data.dptr = key_line;
  144.       key_data.dsize = strlen (key_line)+1;
  145.       return_data = fetch (key_data);
  146.       if (return_data.dptr != NULL)
  147.           printf ("data is ->%s\n\n", return_data.dptr);
  148.       else
  149.         printf ("No such item found.\n\n");
  150.       break;
  151.  
  152.     case 's':
  153.       printf ("key -> ");
  154.       gets (key_line);
  155.       key_data.dptr = key_line;
  156.       key_data.dsize = strlen (key_line)+1;
  157.       printf ("data -> ");
  158.       gets (data_line);
  159.       data_data.dsize = strlen (data_line)+1;
  160.       if (store (key_data, data_data) != 0)
  161.         printf ("Item not inserted. \n");
  162.       printf ("\n");
  163.       break;
  164.  
  165.     case 'd':
  166.       printf ("key -> ");
  167.       gets (key_line);
  168.       key_data.dptr = key_line;
  169.       key_data.dsize = strlen (key_line)+1;
  170.       if (delete (key_data) != 0)
  171.         printf ("Item not found or deleted\n");
  172.       printf ("\n");
  173.       break;
  174.  
  175.     case '1':
  176.       key_data = firstkey ();
  177.       if (key_data.dptr != NULL)
  178.         {
  179.           return_data = fetch (key_data);
  180.           printf ("key  is ->%s\n", key_data.dptr);
  181.           printf ("data is ->%s\n\n", return_data.dptr);
  182.         }
  183.       else
  184.         printf ("No such item found.\n\n");
  185.       break;
  186.  
  187.  
  188.     case '2':
  189.       key_data = nextkey (key_data);
  190.       if (key_data.dptr != NULL)
  191.         {
  192.           return_data = fetch (key_data);
  193.           printf ("key  is ->%s\n", key_data.dptr);
  194.           printf ("data is ->%s\n\n", return_data.dptr);
  195.         }
  196.       else
  197.         printf ("No such item found.\n\n");
  198.       break;
  199.  
  200.     case 'c':
  201.       {
  202.         int temp;
  203.         temp = 0;
  204.         return_data = firstkey ();
  205.         while (return_data.dptr != NULL)
  206.           {
  207.         temp++;
  208.         return_data = nextkey (return_data);
  209.           }
  210.         printf ("There are %d items in the database.\n\n", temp);
  211.       }
  212.       break;
  213.  
  214.     case '?':
  215.       printf ("c - count elements\n");
  216.       printf ("d - delete\n");
  217.       printf ("f - fetch\n");
  218.       printf ("q - quit\n");
  219.       printf ("s - store\n");
  220.       printf ("1 - firstkey\n");
  221.       printf ("2 - nextkey on last return value\n\n");
  222.       break;
  223.  
  224.     default:
  225.       printf ("What? \n\n");
  226.       break;
  227.  
  228.     }
  229.     }
  230.  
  231.   /* Quit normally. */
  232.   exit (0);
  233.  
  234. }
  235.